const Service = require('egg').Service;
const lodash = require('lodash');

class SysFunctionService extends Service {
    /**
     * 新增
     * @param {提交的数据} data 
     * @return 1，0
     */
    async create(data) {
        const { ctx, app } = this;
        let rs = await ctx.model.SysFunction.create(data);
    }

    /**
     * 编辑
     * @param {提交的数据} data 
     * @return 1，0
     */
    async update(data, function_id) {
        const { ctx, app } = this;
        let rs = await ctx.model.SysFunction.update(data, { where: { function_id } });
    }

    /**
     * 查询列表
     * @param {提交的数据} data 
     * @return 1，0
     */
    async querylist(queryParams, pageSize, page) {
        const { ctx, app } = this;
        queryParams = queryParams || {};
        //方便更改,使用 sql 查询,以下步骤为生产sql的过程
        let whereSQL = "where";
        let whereParams = {};
        if (! await app.utils.validator.isEmpty(queryParams['search'])) {
            whereParams['search'] = `%${queryParams['search']}%`;
            if (whereSQL == "where") {
                whereSQL = whereSQL + ' (t.function_name Like :search or t.function_name Like :search)';
            } else {
                whereSQL = whereSQL + ' and (t.function_name Like :search or t.function_name Like :search)';
            }
        }
        if (whereSQL == "where") {
            whereSQL = "where t.deleted='0'"
        } else {
            whereSQL = whereSQL + " and t.deleted='0'"
        }
        //增加页码
        page = (page || 1) * 1;
        pageSize = (pageSize || 10) * 1;
        let dataWhereParams = { ...whereParams };
        dataWhereParams.offset = (page - 1) * pageSize;
        dataWhereParams.limit = pageSize;
        let dataSql = "SELECT * FROM sys_function t " + whereSQL + " LIMIT :offset, :limit";
        let countSql = "SELECT count(*) as total FROM sys_function t " + whereSQL;
        let datalist = await ctx.model.query(dataSql,
            { replacements: dataWhereParams, raw: true, type: app.Sequelize.QueryTypes.SELECT });
        //查询总数
        let count = await ctx.model.query(countSql,
            { replacements: whereParams, raw: true, type: app.Sequelize.QueryTypes.SELECT });
        return {
            datalist,
            totalSize: count[0].total
        }
    }

    /**
     * 查询所有数据，不分页
     */
    async querylistAll() {
        const { ctx, app } = this;
        let dataSql = "SELECT * FROM sys_function t where t.deleted='0'";
        let datalist = await ctx.model.query(dataSql,
            { replacements: {}, raw: true, type: app.Sequelize.QueryTypes.SELECT });
        return datalist;
    }

    /**
     * 删除
     * @param {提交的数据} data 
     * @return 1，0
     */
    async delete(id) {
        const { ctx, app } = this;
        let rs = await ctx.model.SysFunction.destroy({ "where": { function_id: id } });
    }

    /**
     * 删除
     * @param {提交的数据} data 
     * @return 1，0
     */
    async deleteLogic(id) {
        const { ctx, app } = this;
        let rs = await ctx.model.SysFunction.update({ deleted: "1" }, { "where": { function_id: id } });
    }

    /**
     * 查询详情
     * @param {提交的数据} data 
     * @return 1，0
     */
    async queryById(id) {
        const { ctx, app } = this;
        let rs = await ctx.model.SysFunction.findOne({ where: { function_id: id } });
        return rs;
    }
}

module.exports = SysFunctionService;